<?php

function EJCreateOptionListFromDB($table, $keyColumn, $valueColumn, $isSID = true, $condition = "") {
    $list = "";
    $where = (strlen($condition) > 0) ? (" where " . $condition) : "";
    $sql = "select " . $keyColumn . "," . $valueColumn . " from " . $table . $where;
    EJReadTable($sql, function ($row) use ($keyColumn, $valueColumn, $isSID, &$list) {
        if ($isSID) {
            $list .= "<option value='" .
                  $row[$keyColumn] . "'>" .
                  W3GetStringValue($row[$valueColumn]) .
                  "</option>";
        } else {
            $list .= "<option value='" .
                  $row[$keyColumn] . "'>" .
                  $row[$valueColumn] .
                  "</option>";
        }
    });
    
    return $list;
}

function EJCreateBillCategoryCombo($pid = null) {
    return EJCreateOptionListFromDB("billcategory", "ID", "SID");
}

function EJCreateIncomeCategoryCombo($pid = null) {
    return EJCreateOptionListFromDB("incomecategory", "ID", "SID");
}

function EJCreatePaymentModeCombo($pid = null) {
    return EJCreateOptionListFromDB("paymentmode", "ID", "SID");
}

function EJCreateCurrencyCombo($pid = null) {
    return EJCreateOptionListFromDB("currency", "ID", "SID");
}

function EJCreateOwnerCombo($pid = null) {
    $session = W3GetSession();
    $fid = EJGetLoginFamily($session);
    if ($fid == null) {
        W3LogWarning("Failed to get family ID to create owner combo");
        return "";
    }

    return EJCreateOptionListFromDB("person", "ID", "Name", false, "FID=" . $fid);
}

function EJCreateNoteTagCombo($pid = null) {
    return EJCreateOptionListFromDB("notetag", "ID", "SID");
}

function EJCreateNoteTypeCombo($pid = null) {
    return EJCreateOptionListFromDB("notetype", "ID", "SID");
}

 ?>
